(function() {
	var page           = pf.newPage('vehicle_small_List');
	var $el            = null;
	var $porsche_model = null;
	var MYScroll;
	/**
	 * 初始化时调用
	 */
 	var car_list_page_addclass=function(action){
		$("#vehicle_small_List").find("._one_right_list").removeClass("vehicle_small_List_page");
		$("#vehicle_small_List").find("._one_right_list").eq(action).addClass("vehicle_small_List_page");
	}
	page.onCreate = function() {
		$el = page.$el;
		page.big_car=null;
		page.cars_list={
			'911':      25,
			'boxster':  4,
			'cayenne':  6,
			'cayman':   3,
			'macan':    3,
			'panamera': 8
		}
		MYScroll = new iScroll("scroller_small",{
		momentum:     false,
			snap:       true,
			vScroll:    false,
			vScrollbar: false,
			hScrollbar: false,
			click:      true
		});
		$el.on('click','.cars',function(){
			var small_car  = $(this).attr("cars_name");
			var Lang_key   = $(this).attr("small_name");
			var car_tittle = $(this).attr("car_name");

			// if(Lang_key == '911_carrera') {
			// 	pcc2.light('on');
			// }

			// pf.overlay("overlay",{big:page.big_car,small_car:small_car});
			pf.overlay("features",{car_name:small_car,lang_key:Lang_key,tittle:car_tittle,page_car:small_car});
		});

		$el.on('click','.cars_left',function(){
			pcc2.css3(".hi-icon.cars_left");
			$el.find(".ehicle_center_car").css("opacity","1");
			if (Math.abs(MYScroll.currPageX)==0) {
				$el.find($(this)).css("opacity","0");
			}
			if (Math.abs(MYScroll.currPageX)==1) {
				$el.find($(this)).css("opacity","0");
			}
			if (Math.abs(MYScroll.currPageX)==0) {
			}else if(!tf(Math.abs(MYScroll.currPageX))){
				MYScroll.scrollToPage(Math.abs(MYScroll.currPageX)-1,0,500);
				// alert((MYScroll.currPageX));
				car_list_page_addclass(MYScroll.currPageX);
			}
		});
		$el.on('click','.cars_right',function(){
			pcc2.css3(".hi-icon.cars_right");
			$el.find(".ehicle_center_car").css("opacity","1");
		
			if ($(this).attr("car_type")=="911") {
				if (Math.abs(MYScroll.currPageX)+2>4) {
					$el.find($(this)).css("opacity","0");
				}
				if (Math.abs(MYScroll.currPageX)==4) {
				// $el.find($(this)).css("opacity","0.5");
				}else if(!tf(Math.abs(MYScroll.currPageX))){
					MYScroll.scrollToPage(MYScroll.currPageX+1,0,500);
					car_list_page_addclass(MYScroll.currPageX);
				}
			}else if($(this).attr("car_type")=="panamera"){
				if (Math.abs(MYScroll.currPageX)+2>1) {
					$el.find($(this)).css("opacity","0");
				}
				if (Math.abs(MYScroll.currPageX)==1) {
				// $el.find($(this)).css("opacity","0.5");
				}else if(!tf(Math.abs(MYScroll.currPageX))){
					MYScroll.scrollToPage(MYScroll.currPageX+1,0,500);
					car_list_page_addclass(MYScroll.currPageX);
				}
			}else{
				if (Math.abs(MYScroll.currPageX)+2>4) {
					$el.find($(this)).css("opacity","0");
				}
			}
		});
		$porsche_model = $el.find('.porsche_model');
		$('.ehicle_center_car').hide();
	};

	function showImgTitle(cardID) {
		var image = util.format('url(./image/stock/text/stock_%s_text.png)', cardID);
		$porsche_model.css('background-image', image);
	};

	/**
	 * 页面显示时自动调用
	 */
	function tf(es){
	var r= /^[+-]?[1-9]?[0-9]*\.[0-9]*$/;
    return r.test(es);
	}
	function all_will_hide(){
		left_hide();
		right_hide();
	}
	function left_hide(){
		if (MYScroll.x==0) {
			$('.cars_left').hide();
		};
	}
	function right_hide(){
		if (MYScroll.x==0) {
			$('.cars_right').hide();
		};
	}
	page.onShow = function(params) {
		window.setTimeout(function() {
			MYScroll.refresh();
		}, 1000);
		var cars_name={
			boxster:0,
			cayman:1,
			"911":2,
			panamera:3,
			macan:4,
			cayenne:5
		}
		car_id=cars_name[params.car_name];
		$('.ehicle_center_car').attr('car_type',params.car_name);
		// alert(params.car_name);
		// all_will_hide();
		if (params.car_name=="911") {
			$('.ehicle_center_car').show();
		};
		if (params.car_name=="panamera") {
			$('.ehicle_center_car').show();
		};
		
		var iScroll_length=page.cars_list[params.car_name]/6;
		if(tf(iScroll_length)){
        iScroll_length=parseInt(iScroll_length);
        iScroll_length=iScroll_length+1;
    }
    for (var i = 0; i < iScroll_length; i++) {
    	$('.iscroller_box').append('<table class="_one_right_list"><tr><td></td></tr></table>')
    };
    //\得到页数
    $el.find('.iscroller_box').css('width',((1720*iScroll_length)+20)+'px');
		// $el.show();
		var action = params.car_name;
		var List_index = 0;
		for (var i = 0; i <page.cars_list[params.car_name]; i++) {
			if ((i+1)%6==1) {
	      if (i>5) {
	        List_index=List_index+1;
	      };
	    };
			// $el.find("._one_right_list").eq(List_index).find('td').append('<div class="cars" small_name='+ehicleStock[car_id]["submodels"][i].car_lang+' car_name='+ehicleStock[car_id]["submodels"][i].name+'  cars_name='+action+' style="background-image:url(./image/stock/cars/stock_'+action+'_'+[i+1]+'.png)"><div class="cars_tittle">'+ehicleStock[car_id]["submodels"][i].name+'</div></div>');
			var $dom = $el.find("._one_right_list").eq(List_index).find('td');
			var tpl = '<div class="cars" small_name="%s" car_name="%s" cars_name="%s" style="background-image:url(./image/stock/cars/stock_%s.png)"><div class="cars_tittle">%s</div></div>';
			var item = ehicleStock[car_id]["submodels"][i];
			var html = util.format(tpl, item.car_lang, item.name, action, action+'_'+[i+1], item.name);
			$dom.append(html);
		};
		page.big_car=action;

		showImgTitle(params.car_name);
		car_list_page_addclass(0);
	};

	/**
	 * 页面隐藏时调用
	 */
	page.onHide = function() {
		// $el.hide();
		$el.find(".ehicle_center_car").css("opacity","1");
		$el.find($('.cars_left')).css("opacity","0");
		$('.ehicle_center_car').hide();
		MYScroll.scrollToPage(0);
		car_list_page_addclass(0);
		MYScroll.refresh();
		$el.find('.iscroller_box').empty();
		$el.find("._one_right_list").empty();
	};

})();